package com.leonzx.base.controller.layim;

import com.alibaba.fastjson.JSONArray;
import com.leonzx.base.constants.IMConstants;
import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.im.ImFriendGroupUserData;
import com.leonzx.base.entity.im.ImFriendUserInfoData;
import com.leonzx.base.entity.im.ImGroupUserData;
import com.leonzx.base.entity.im.ImUserData;
import com.leonzx.base.entity.system.User;
import com.leonzx.base.service.im.ImService;
import com.leonzx.base.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author:李晓雷
 * @Description:
 * @Date: 18/07/13 13:32
 */
@Controller
@RequestMapping("/layim")
public class LayimController extends BaseController {
    @Value("${jim.serviceIp}")
    private String serviceIp;
    @Autowired
    private ImService imService;
    @Value("${jim.port}")
    public Integer port;

    @Autowired
    private UserService userService;

    /**
     * 静态模版所在路径前缀
     */
    private String prefix = "layim/pc";
    private String layimPrefix = "layim/orders";

    /**
     * PC端即时通讯获取人员信息
     *
     * @return
     */
    @RequestMapping("/pc/getImUser")
    @ResponseBody
    public Object gerImUser(String userId) {
        ImUserData imUserData = new ImUserData();
        try {
            Map<String, Object> map = new HashMap<>();
            User currentUser = userService.getUserById(userId);
            ImFriendUserInfoData mine = new ImFriendUserInfoData();
            //1设置自己信息
            mine.setId(currentUser().getId());
            //默认头像
            mine.setAvatar("/static/im/images/user.png");
            mine.setSign(currentUser.getImSign());
            mine.setUsername(currentUser.getName());
            mine.setStatus(Objects.equals(currentUser.getImStatus(), IMConstants.IM_STATUS_OFFLINE) ? IMConstants.IM_STATUS_HIDE : currentUser.getImStatus());
            map.put("mine", mine);

            //2设置群组信息
            //业务是默认人员进入所有委托state = 1的群组
            List<ImGroupUserData> groups = imService.getGroups();
            map.put("group", groups);

            //3.设置好友信息
            List<ImFriendGroupUserData> friends = imService.queryGroupAndUser(userId);
            map.put("friend", friends);

            //4.设置成功返回头
            imUserData.setCode("0");
            imUserData.setMsg("获取成功");
            imUserData.setData(map);
        } catch (Exception e) {
            imUserData.setCode("1");
            imUserData.setMsg("获取失败");
        }
        return JSONArray.toJSON(imUserData);
    }

    /**
     * Pc端获取历史聊天内容
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/pc/historymessage")
    public String historymessage(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        model.addAttribute("receiveId", id);
        model.addAttribute("type", type);
        model.addAttribute("userId", currentUser().getId());
        return prefix + "/history-message";
    }

    /**
     * 加入群组聊天
     *
     * @return
     */
    @RequestMapping("/orders/joinGroup")
    public String joinGroup(String id, String number, Model model) throws UnknownHostException {
        model.addAttribute("serviceIp", serviceIp);
        model.addAttribute("port", port);
        model.addAttribute("number", number);
        model.addAttribute("userId", currentUser().getId());
        model.addAttribute("name", currentUser().getName());
        model.addAttribute("userName", currentUser().getUsername());
        model.addAttribute("group_id", id);
        return layimPrefix + "/chat-root";
    }

    /**
     * 图片展示
     *
     * @param imgUrl
     * @param model
     * @return
     */
    @RequestMapping("/orders/showImg")
    public String showImg(String imgUrl, Model model) {
        model.addAttribute("imgUrl", imgUrl);
        return layimPrefix + "/show-img";
    }

}
